;; core/mthread.scm - (c) rohan drape, 2003-2005

;; An <mthread> is a managed thread.

;; The list of managed threads.

(define-global mthread-data '())

;; Make a managed thread.

(define (make-mthread p)
  (let ((t (make-thread p)))
    (mthread-data (cons t (mthread-data)))
    t))

(define (make-mthread* p)
  (let ((t (make-thread* p)))
    (mthread-data (cons t (mthread-data)))
    t))

;; Evaluate `body ...' in a new managed thread.

(define-syntax in-mthread 
  (syntax-rules ()
    ((_ body ...)
     (make-mthread* (lambda () body ...)))))

;; Terminate all managed threads.

(define (mthread-terminate-all!)
  (for-each thread-terminate! (mthread-data))
  (mthread-data '()))
